<?php
namespace Admin\Controller;


use Admin\Model\Page;
use Think\Exception;

class PrivilegeController extends BaseController
{
    public function __construct($baseModel)
    {
        parent::__construct(D('Privilege'));
        $this->bcItemPush('权限列表', U('Privilege/index'));
    }

    private static $_validate = array(
        array('name', 'require', '权限名称不能为空！', 1, 'regex', 3),
//        array('url', 'require', '权限URL不能为空！', 1, 'regex', 3),
    );

    public function index()
    {
        $model = D('Privilege');
        //构建查询条件
        $pagesize = 10;
        $name = I('name');
        $id = I('id');
        $pagenumber = I('pagenum', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($name != '') {
            $map['t_privilege.name'] = array('like', '%' . $name . '%');
        }
        if ($id != '') {
            $map['t_privilege.id'] = $id;
        }
        //查询
        $list = $model->getByConditionPage($map,$pagenumber,$pagesize);
        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        //显示数据
        $this->assign('page', $page);
        $this->assign('name', $name);
        $this->assign('id', $id);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('privilegeList', $list);
        $this->assign('recordcount', $recordcount);
        $this->display();
    }

    public function addUI()
    {
        $PrivilegeList = $this->baseModel->getTree();
        $this->bcItemPush('添加权限', U('Privilege/addUI'));
        $this->assign('privilegeList', $PrivilegeList);
        $this->display('save');
    }

    protected function doAddBefore($data)
    {
        $this->checkSave($data, '添加');
    }


    public function add()
    {
        parent::add(self::$_validate);
    }

    public function editUI($id)
    {
        $PrivilegeList = $this->baseModel->getTree();
        $where['id'] = ':id';
        $this->baseModel->where($where)->bind($where['id'], $id)->find();
        $this->bcItemPush('编辑权限', U('Privilege/editUI/'.$id));
        $this->assign('privilege', $this->baseModel->data());
        $this->assign('privilegeList', $PrivilegeList);
        $this->display('save');
    }

    protected function doEditBefore($data)
    {
        $this->checkSave($data, '修改');
    }

    public function edit()
    {
        parent::edit(self::$_validate);

    }

    public function doDeleteBefore($id)
    {
        //级联删除关联表
        $where['pri_id'] = ":pri_id";
        $bind[':pri_id'] = array($id, \PDO::PARAM_INT);
        $RolePrivilege = D('role_privilege');
        $RolePrivilege->where($where)->bind($bind)->delete();
    }

    public function delete($id)
    {
        parent::delete($id);
    }

    /**
     * @param $data
     * @param $option 当前操作
     */
    private function checkSave($data, $option)
    {
        $this->checkName($data, $option);
        $this->checkURL($data, $option);
        if ($data['parent_id'] == $data['id']) {
            parent::retJson(false, $option . '失败，不能将当前权限作为父权限');
        }
        if ($option == '修改') {
            $parentPrivilege = $this->baseModel->find($data['parent_id']);
            if ($parentPrivilege['parent_id'] == $data['id']) {
                parent::retJson(false, $option . '失败，权限应该为树形结构');
            }
        }
    }

    /**
     * 权限URL不能重复
     * @param $data
     * @param $option
     */
    private function checkURL($data, $option)
    {
        $where['url'] = ':url';
        $count = $this->baseModel->where($where)->bind($where['url'], $data['url'])->count();
//        if (($option == '修改' && $count >= 2) || ($option == '添加' && $count >= 1)) {
//            parent::retJson(false, $option . '失败，权限URL不能重复');
//        }
    }

    /**
     * 权限名称不能重复
     * @param $data
     * @param $option
     */
    private function checkName($data, $option)
    {
        $where['name'] = ':name';
        $count = $this->baseModel->where($where)->bind(':name', $data['name'])->count();
        if (($option == '修改' && $count >= 2) || ($option == '添加' && $count >= 1)) {
            parent::retJson(false, $option . '失败，权限名称不能重复');
        }
    }
}